package com.oplink.webbase.modules.cfg.services;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oplink.webbase.common.persistence.Page;
import com.oplink.webbase.modules.cfg.dao.CfgCardValueDao;
import com.oplink.webbase.modules.cfg.dao.CfgSiteDao;
import com.oplink.webbase.modules.cfg.entity.CfgCardValue;
import com.oplink.webbase.modules.cfg.entity.CfgSite;

/**
 * 
 * Title: 保存板卡参数值
 * Description:
 * Copyright: Copyright (c) 2014
 * Company: Oplink Communications
 * 
 * @author
 * @serialData 2014-9-23 上午8:39:26
 * @version 1.0
 */
@Component
@Transactional
public class CfgCardValueService {

	@Autowired
	private CfgCardValueDao cfgValueDao;

	@Autowired
	private CfgSiteDao siteDao;

	public Page<CfgCardValue> find(Page<CfgCardValue> page, CfgCardValue card) {

		DetachedCriteria dc = cfgValueDao.createDetachedCriteria();
		// 添加查询条件
		if (StringUtils.isNotEmpty(card.getField())) {
			dc.add(Restrictions.like("cardname", "%" + card.getField() + "%"));
		}
		/* dc.add(Restrictions.eq(Index.DEL_FLAG, Index.DEL_FLAG_NORMAL)); */
		dc.addOrder(Order.desc("id"));
		return cfgValueDao.find(page, dc);
	}

	public List<CfgSite> getAllSite() {
		return (List<CfgSite>) siteDao.findAll();
	}

	public CfgCardValue getCardByType(String type) {
		DetachedCriteria dc = cfgValueDao.createDetachedCriteria();
		// 添加查询条件
		if (StringUtils.isNotEmpty(type)) {
			dc.add(Restrictions.eq("type", type));
		}
		List<CfgCardValue> listCards = cfgValueDao.find(dc);
		if ((listCards != null) && (listCards.size() > 0)) {
			return listCards.get(0);
		}
		return null;
	}

	public CfgCardValue getValue(Long id) {
		return cfgValueDao.findOne(id);
	}

	public void saveValue(CfgCardValue entity) {
		cfgValueDao.save(entity);
	}

	public void delete(Long id) {
		cfgValueDao.delete(id);
	}

	@Autowired
	public void setCfgCardValueDao(CfgCardValueDao cfgDao) {
		this.cfgValueDao = cfgDao;
	}
}
